package com.dhp.security.config;

import com.dhp.security.filter.TokenAuthenticationFilter;
import com.dhp.security.filter.TokenLoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @ClassName: SecurityConfig
 * @Description: springsecurity的配置类
 * @Author: dhp
 * @Date: 2020/12/5 14:41
 * @Version: v1.0
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserDetailsService userDetailsService;
    @Autowired
    BCryptPasswordEncoder passwordEncoder;


    //指定我们自定义的userDetailsService,还有加密方式
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //未授权的统一处理
        http.exceptionHandling().authenticationEntryPoint(new UnauthorizedEntryPoint())
                //关闭csrf防护机制,让所有的请求都要有权限才能访问
                .and().csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                //指定退出的地址，制定退出后的规则（删除token和redis里的权限数据）
                .and().logout().logoutUrl("/admin/acl/index/logout")
                .addLogoutHandler(new TokenLogoutHandler(redisTemplate))
                //添加关于自定义的认证过滤器和自定义的授权过滤器
                .and()
                .addFilter(new TokenLoginFilter(authenticationManager(),redisTemplate))
                .addFilter(new TokenAuthenticationFilter(authenticationManager(),redisTemplate)).httpBasic();
    }

    /**
     * 配置哪些请求不拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(
                        "/swagger-resources/**",
                        "/PearAdmin/**",
                        "/component/**",
                        "/user/**",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/swagger-ui.html",
                        "/webjars/**",
                        "/v2/**",
                        "/druid/**");
    }

}
